<template>
  <div>
    <table
      border="1"
      width="700"
      style="border-collapse: collapse"
    >
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th>
            <input v-model="isAll" type="checkbox" /> <span>全选</span>
          </th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <!-- 1,静态铺设 -->
      <!-- 1.用对象循环 -->
      <!-- 2，全选，小选 v-model双向数据绑定 -->
      <tbody>
          <!-- :name="obj.name"
        :price="obj.price"
        :checked="obj.checked"
        :count="obj.num" -->
        <tr 
        is="Tr"
        v-for="(obj,index) in goodList"
        :key="index"
        :obj="obj"
        @del="delFn"
        :index="index"
        >
       
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5">
            <!-- 计算属性 -->
            <span>{{allPrice}}</span>
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import Tr from './components/Tr'
export default {
  components:{
    Tr
  },
  data() {
    return {
      goodList: [
        {
          name: "诸葛亮",
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: "蔡文姬",
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: "妲己",
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: "鲁班",
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
    };
  },
  // 全选影响小选
  computed:{
    isAll:{
      set(val){//页面点击会选中状态true / false 赋予给v-modle的变量
        this.goodList.forEach(obj => obj.checked=val);
      },
      get(){
        return this.goodList.every(obj=>obj.checked==true)
      }
    },
    allPrice(){
      return this.goodList.reduce((sum,obj)=>{
        //只有选中状态才会合计
        if (obj.checked==true) {
          sum+=obj.price*obj.num
        } 
        return sum
      },0)
    }
  },
  methods:{
  
    delFn(index){
      this.goodList.splice(index,1)
    }
  }
};
</script>

<style>
span {
  color: red;
}
</style>